package StudyCode.summary.regularexpressionsmatch;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class UsingPatternAndMatcherClasses {
	public static void main(String[] args) {
		String regex = "[abc]@.";

		String source = "cric@mypkg.com is a valid email address";
		UsingPatternAndMatcherClasses.findPattern(regex, source);

		source = "asdf@mypkg.com is invalid";
		UsingPatternAndMatcherClasses.findPattern(regex, source);

		source = "a@asdf@fdsa@u";
		UsingPatternAndMatcherClasses.findPattern(regex, source);

		source = "There is an @ sign here";
		UsingPatternAndMatcherClasses.findPattern(regex, source);
	}

	public static void findPattern(String regex, String source) {
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(source);
		boolean found = false;

		System.out.println("\n正则表达式:" + regex);
		System.out.println("文本:" + source);

		while (m.find()) {
			System.out.println("匹配文本:" + m.group() + ", 开始:" + m.start() + ", " +
					"结尾:" + m.end());
			found = true;
		}

		if (!found) {
			System.out.println("没有匹配结果");
		}
	}
}
